package game.lan;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.module.ddz.bean.DdzLanPlayerInfo;
import game.module.ddz.logic.DdzDataManager;
import game.module.ddz.logic.DdzLogic;
import game.module.mj.bean.MjLanPlayerInfo;
import game.module.mj.logic.MjDataManager;
import game.module.mj.logic.MjLogic;
import game.module.nn.logic.NnDataManager;
import game.module.nn.logic.NnLogic;
import game.module.qzmj.bean.QzmjLanPlayerInfo;
import game.module.qzmj.logic.QzmjLogic;
import game.module.sss.logic.SssDataManager;
import game.module.sss.logic.SssLogic;
import game.module.user.dao.LanSessionCache;
import game.module.user.logic.RoomDataManager;
import io.netty.channel.Channel;
import lion.lan.ILanIoExecutor;
import lion.lan.Netty4LanServer;
import lion.netty4.message.RequestMessage;
import lion.netty4.message.SendToMessage;

public class RoomLanServerManager implements ILanIoExecutor {

	private static Logger logger = LoggerFactory.getLogger(RoomLanServerManager.class);

	private Netty4LanServer lanServer;

	static class SingletonHolder {
		static RoomLanServerManager instance = new RoomLanServerManager();
	}

	public static RoomLanServerManager getInstance() {
		return SingletonHolder.instance;
	}

	private RoomLanServerManager() {
	}

	public void startServer(int port) {
		try {
			lanServer = new Netty4LanServer(port);
			lanServer.run(Runtime.getRuntime().availableProcessors(), this, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void shutdown() {
		if (lanServer != null) {
			lanServer.shutdown();
		}
	}

	@Override
	public void execute(final Channel channel, RequestMessage requestMessage) throws IOException {
		int msgCode = requestMessage.getMsgCode();
		logger.info("receive lan message ,msgCode={}", msgCode);
		// ip判断
		// Set<String> allowIpSet =
		// HallServer.getInstance().getServerConfig().getLanAllowIps();
		// if (channel.remoteAddress() != null) {
		// String ipStr = channel.remoteAddress().toString();
		// String aIp = StringUtils.split(ipStr, ":")[0].substring(1);
		// if (!allowIpSet.contains(aIp)) {
		// logger.warn("not valid ip lan request,ipAddr={}", ipStr);
		// return;
		// }
		// }
		switch (requestMessage.getMsgCode()) {
		case 301:// ddz room start
			String roomUuid = requestMessage.readString();
			int playerCount = requestMessage.readInt();
			List<DdzLanPlayerInfo> reqList = new ArrayList<>();
			for (int i = 0; i < playerCount; i++) {
				int playerId = requestMessage.readInt();
				String playerName = requestMessage.readString();
				int playerSex = requestMessage.readInt();
				String headImg = requestMessage.readString();
				// 添加cache
				RoomDataManager.getInstance().addOrUpdatePlayer(playerId, playerName, playerSex, headImg, 0);
				Long sessionId = requestMessage.readLong();
				reqList.add(new DdzLanPlayerInfo(sessionId, playerId));
			}
			short roundSum = requestMessage.readShort();
			byte jiaoOrQiang = requestMessage.readByte();
			DdzLogic.getInstance().ddzStart(channel, roomUuid, reqList, roundSum, jiaoOrQiang);
			break;
		case 303:// check in room
			roomUuid = requestMessage.readString();
			short round_sum = requestMessage.readShort();
			byte roomFeeType = requestMessage.readByte();
			int roomFee = requestMessage.readInt();
			byte nnis_jindou = requestMessage.readByte();
			int nnjindou = requestMessage.readInt();
			playerCount = requestMessage.readInt();
			reqList = new ArrayList<>();
			for (int i = 0; i < playerCount; i++) {
				int playerId = requestMessage.readInt();
				String playerName = requestMessage.readString();
				int playerSex = requestMessage.readInt();
				String headImg = requestMessage.readString();
				// 添加cache
				RoomDataManager.getInstance().addOrUpdatePlayer(playerId, playerName, playerSex, headImg, 0);
				Long sessionId = requestMessage.readLong();
				reqList.add(new DdzLanPlayerInfo(sessionId, playerId));
			}
			NnLogic.getInstance().generateCards(channel, roomUuid, reqList, round_sum, roomFeeType, roomFee,
					nnis_jindou, nnjindou);
			break;
		case 305:
			roomUuid = requestMessage.readString();
			short roundCount = requestMessage.readShort();
			byte antiCheet = requestMessage.readByte();
			byte mapai = requestMessage.readByte();
			byte daqiang = requestMessage.readByte();
			byte sunzi_a = requestMessage.readByte();
			byte baodao = requestMessage.readByte();
			byte is_jindou = requestMessage.readByte();
			int jindoudi = requestMessage.readInt();
			int fees = requestMessage.readInt();
			byte roomfeetye = requestMessage.readByte();
			byte player_Count = requestMessage.readByte();
			byte[] add_color = requestMessage.readBytes();
			playerCount = requestMessage.readInt();
			reqList = new ArrayList<>();
			for (int i = 0; i < playerCount; i++) {
				int playerId = requestMessage.readInt();
				String playerName = requestMessage.readString();
				int playerSex = requestMessage.readInt();
				String headImg = requestMessage.readString();
				Long jindou = requestMessage.readLong();
				// 添加cache
				RoomDataManager.getInstance().addOrUpdatePlayer(playerId, playerName, playerSex, headImg, jindou);
				Long sessionId = requestMessage.readLong();
				reqList.add(new DdzLanPlayerInfo(sessionId, playerId));

			}
			SssLogic.getInstance().generateCards(channel, roomUuid, roundCount, antiCheet, mapai, daqiang, sunzi_a,
					baodao, is_jindou, jindoudi, roomfeetye, fees, reqList, player_Count, add_color);
			break;
		case 307:
			roomUuid = requestMessage.readString();
			roundCount = requestMessage.readShort();
			byte dapai = requestMessage.readByte();
			byte typehu = requestMessage.readByte();
			byte cheat = requestMessage.readByte();
			int gold_rate = requestMessage.readInt();
			byte cost_type = requestMessage.readByte();
			playerCount = requestMessage.readInt();
			// reqList = new ArrayList<>();
			List<MjLanPlayerInfo> reqList1 = new ArrayList<>();
			for (int i = 0; i < playerCount; i++) {
				int playerId = requestMessage.readInt();
				String playerName = requestMessage.readString();
				int playerSex = requestMessage.readInt();
				String headImg = requestMessage.readString();
				// 添加cache
				RoomDataManager.getInstance().addOrUpdatePlayer(playerId, playerName, playerSex, headImg, 0);
				Long sessionId = requestMessage.readLong();
				reqList1.add(new MjLanPlayerInfo(sessionId, playerId));
			}
			// short roundSum1 = requestMessage.readShort();
			MjLogic.getInstance().mjStart(channel, roomUuid, reqList1, roundCount, dapai, typehu, cost_type);
			break;
		case 309:
			int opType = requestMessage.readInt();
			int playerId = requestMessage.readInt();
			int roomType = RoomDataManager.getInstance().getRoomType(playerId);
			Long sessionId = LanSessionCache.getInstance().getSessionIdByPlayer(playerId);
			sessionId = sessionId != null ? sessionId : 0;
			SendToMessage send2HsMessage = new SendToMessage(channel.alloc(), 310);
			send2HsMessage.writeInt(opType);
			send2HsMessage.writeInt(playerId);
			send2HsMessage.writeInt(roomType);
			send2HsMessage.writeLong(sessionId);
			channel.writeAndFlush(send2HsMessage);
			break;
		case 311:
			int ddzCount = DdzDataManager.getInstance().getPlayerCount();
			int nnCount = NnDataManager.getInstance().getPlayerCount();
			int sssCount = SssDataManager.getInstance().getPlayerCount();
			int mjCount = MjDataManager.getInstance().getPlayerCount();
			send2HsMessage = new SendToMessage(channel.alloc(), 312);
			send2HsMessage.writeInt(ddzCount);
			send2HsMessage.writeInt(nnCount);
			send2HsMessage.writeInt(sssCount);
			send2HsMessage.writeInt(mjCount);
			channel.writeAndFlush(send2HsMessage);
			break;
		case 313:
			roomUuid = requestMessage.readString();
			roundCount = requestMessage.readShort();
			byte sanjindao = requestMessage.readByte();
			byte eat_card = requestMessage.readByte();
			byte hua_card = requestMessage.readByte();
			byte prevent_cheat = requestMessage.readByte();
			int gold_coin = requestMessage.readInt();
			byte cost_way = requestMessage.readByte();
			byte youjin = requestMessage.readByte();
			short fee = requestMessage.readShort();
			playerCount = requestMessage.readInt();
			// reqList = new ArrayList<>();
			List<QzmjLanPlayerInfo> QzreqList = new ArrayList<>();
			Map<Integer,String> ipmap = new HashMap<Integer,String>();
			for (int i = 0; i < playerCount; i++) {
				int playerid = requestMessage.readInt();
				String playerName = requestMessage.readString();
				int playerSex = requestMessage.readInt();
				String headImg = requestMessage.readString();
				// 添加cache
				RoomDataManager.getInstance().addOrUpdatePlayer(playerid, playerName, playerSex, headImg, 0);
				ipmap.put(playerid, requestMessage.readString());
				Long sessionid = requestMessage.readLong();
				QzreqList.add(new QzmjLanPlayerInfo(sessionid, playerid));
			}
			// short roundSum1 = requestMessage.readShort();
			QzmjLogic.getInstance().mjStart(channel, roomUuid, QzreqList, roundCount, sanjindao, eat_card, hua_card,
					prevent_cheat, gold_coin, cost_way, youjin, fee,ipmap);
			break;
		default:
			break;
		}
	}

}
